﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace BanVeXemPhim_Presentation
{
    class ThongKeDAO : DataProvider
    {
        public ThongKeDTO[] ThongKeTheoTenPhim(ThongKeDTO thongkeDTO)
        {
            connect();
            string query = "SELECT Phim.TenPhim as TenPhim, sum(giave.giave) as DoanhThu, xuatchieu.ngaychieu as NgayChieu " +
                           "FROM Phim INNER JOIN PhienBan ON Phim.IdPhienBan=PhienBan.IdPhienBan JOIN GiaVe ON phienban.idphienban = giave.idphienban"
                           +" Join XuatChieu on phim.idphim = xuatchieu.phim Join Ve on Ve.xuatchieu = xuatchieu.idxuatchieu "
                           +"where phim.tenphim = " +thongkeDTO.TenPhim
                           +" Group by phim.tenphim";
            MessageBox.Show(query);
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet(); 
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            ThongKeDTO[] dsThongKe = new ThongKeDTO[n];
            for (int i = 0; i < n; i++)
            {
                dsThongKe[i] = (ThongKeDTO)GetDataFromDataRow(dataTable, i);
            }

            return dsThongKe;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ThongKeDTO thongkeDTO = new ThongKeDTO();
            thongkeDTO.TenPhim = dt.Rows[i]["TenPhim"].ToString();
            thongkeDTO.NgayChieu = Convert.ToDateTime(dt.Rows[i]["NgayChieu"].ToString());
            thongkeDTO.TongDoanhThu = int.Parse(dt.Rows[i]["DoanhThu"].ToString());
            return thongkeDTO;
        }
    }
}
